package com.wanchuang.shengfubao.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;


import com.wanchuang.shengfubao.BuildConfig;
import com.wanchuang.shengfubao.help.HyphenateLibraryCallBackHandle;
import com.wanchuang.shengfubao.help.MyActivityLifecycle;
import com.wanchuang.shengfubao.help.UserHelper;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.example.havi.helper.ChatHelper;
import com.wanchuang.shengfubao.push.JPushHelper;


import java.util.ArrayList;

public class MainApplication extends Application {

    public static ChatHelper getChatHelper(){
        return chatHelper;
    }

    private final static ArrayList<BaseActivity> activitys = new ArrayList<>();
    private final static ArrayList<Activity> tempActivitys = new ArrayList<>(); //临时记录activity界面
    private final static ArrayList<Activity> webActivityList = new ArrayList<>();
    private static MainApplication instance;
    private static MyActivityLifecycle myActivityLifecycle;
    public static  boolean isRef = false;

    public static String getServiceHost() {
        return serviceHost;
    }

    public static void setServiceHost(String serviceHost) {
        MainApplication.serviceHost = serviceHost;
    }

    private static String serviceHost = BuildConfig.SERVERHEAD;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        myActivityLifecycle = new MyActivityLifecycle();
        registerActivityLifecycleCallbacks(myActivityLifecycle);    //监听activity生命周期
        NetworkManager.INSTANCE.init(this); //初始化网络请求
        initJPush();
        initHyphenateIM();
    }

    private static ChatHelper chatHelper;
    private void initHyphenateIM() {
        //初始化
        chatHelper = ChatHelper.getInstance();
        chatHelper.init(this,new HyphenateLibraryCallBackHandle());
        //同步用户账号与环信账号信息 项目使用中应该判断是否登录
    }


    private void initJPush() {
        JPushHelper.init(this);
        if(UserHelper.isLogin()){
            JPushHelper.setAlias(UserHelper.getUser().getPush_id());
        }
    }



    public static MainApplication getInstance() {
        return instance;
    }
    public static Context getAppContext() {
        return instance.getApplicationContext();
    }





    /**
     * 判断前台后台运行
     * @return  true  处于后台   false  前台
     */
    public static boolean isAppBackground() {
        if (myActivityLifecycle.getStartCount() == 0) {
            return true;
        }
        return false;
    }


    /**
     * 显示登录凭证失效弹窗提醒在当前显示的界面上
     */
    public static void showTokenInvalidHint(){
        BaseActivity activity = activitys.get(activitys.size()==0?0:activitys.size() - 1);
        UserHelper.loginOut();
        activity.showTokenInvalidHintDialog();
    }

    //全局记录activity
    public static void addActivity(BaseActivity context) {
        activitys.add(context);
    }
    public static void removeActivity(BaseActivity context) {
        activitys.remove(context);
    }
    public static void finishAllActivity() {
        Activity activity;
        for (int i = activitys.size() - 1; i >= 0; i--) {
            activity = activitys.get(i);
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }
    public static void finishTopActivity(int number)
    {
        Activity activity;
        for (int i = activitys.size() - 1; i >= activitys.size() - number; i--) {
            activity = activitys.get(i);
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    //临时记录activity
    public static void addTempActivity(Activity context) {
        tempActivitys.add(context);
    }
    public static void removeTempActivity(Activity context) {
        tempActivitys.remove(context);
    }
    public static void finishAllTempActivity() {
        Activity activity;
        for (int i = tempActivitys.size() - 1; i >= 0; i--) {
            activity = tempActivitys.get(i);
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    //网页记录activity
    public static void addWebActivity(Activity context) { //记录打开的activity
        webActivityList.add(context);
    }
    public static void removeWebActivity(Activity key) {
        webActivityList.remove(key);
    }
    public static void finishAllWebActivity() {
        Activity activity;
        for (int i = webActivityList.size() - 1; i >= 0; i--) {
            activity = webActivityList.get(i);
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }


}
